/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.SetuBillItemDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class SetuBillItemDaoImpl extends BaseDaoImpl implements SetuBillItemDao {
	public List getListBySetuBill(long billId) {
		String sql = "from com.minierp.model.SetuBillItem s where s.billId="
				+ billId + " order by s.id";
		return this.getHibernateTemplate().find(sql);
	}

	public List getListByBusinessBill(long billId) {
		String sql = "select b,s,bbItem from com.minierp.model.SetuBill b ,com.minierp.model.SetuBillItem s ,com.minierp.model.BusinessBillItem bbItem where b.id=s.billId and s.bbItemId=bbItem.id and b.enab=\'Y\' and b.bbId="
				+ billId + " order by s.id";
		return this.getHibernateTemplate().find(sql);
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.SetuBillItem where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public BigDecimal getSetuAmount(long bbItemId) {
		String sql = "select sum(i.amount) from com.minierp.model.SetuBill b ,com.minierp.model.SetuBillItem i where b.id=i.billId and i.bbItemId="
				+ bbItemId + " and b.enab=\'Y\'";
		List list = this.getHibernateTemplate().find(sql);
		return list.size() > 0 && list.get(0) != null ? (BigDecimal) list
				.get(0) : new BigDecimal(0);
	}
}